/*
 * @Author: Gary 
 * @Date: 2018-09-28 17:36:16 
 * @Last Modified by: Gary
 * @Last Modified time: 2018-10-09 16:44:24
 * 列表页刷新判断
 * 内置refresh方法用以刷新所做操作，当判断为需要刷新时，会调用该方法
 */

import Store from '@/store'

const listRefresh = {
  beforeRouteEnter(to, from, next) {
    let tagList = Store.getters.cachedViews, routeName = to.name, flag = true;
    for (let i = 0; i < tagList.length; i++) {
      if (tagList[i] == routeName) {
        flag = false;
      }
    }
    next(vm => {
      vm.reload = flag;
    })
  },
  data() {
    return {
      reload: true,
    }
  },
  activated() {
    setTimeout(() => {
      if (this.refresh) {
        if (this.reload) {
          this.$store.commit('DEL_LIST_REFRESH', this.$route.path);
          this.refresh();
          console.log('需要刷新页面-新开tag')
        } else if (this.$store.getters.listRefresh.indexOf(this.$route.path) !== -1) {
          this.$store.commit('DEL_LIST_REFRESH', this.$route.path);
          this.refresh();
          console.log('需要刷新页面-自详情页返回')
        }
      }
    })
  }
}

export default listRefresh